package com.weidian.mobile.open.scheduler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.weidian.mobile.open.utils.HttpUtils;

@Component
public class EasemobTokenJob {

	@Autowired
	StringRedisTemplate jedisTemplate;
	
	public static final long cacheTime = 1000 * 60 * 60 * 24;
	
	@Scheduled(initialDelay=1000, fixedRate=cacheTime)
	public void refreshEasemobToken(){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("grant_type", "client_credentials");
		jsonObject.put("client_id", "YXA65gUuAPwoEeeTdatV-TJOvQ");
		jsonObject.put("client_secret", "YXA6eEI63jLr0A6lktRBne8YetxTBUI");
		String ret = null;
		try {
			ret = HttpUtils.sendPostforEasemob("https://a1.easemob.com/1119161217115396/wonsionton/" + "token", jsonObject.toJSONString(), null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jsonObject = JSON.parseObject(ret);
		String token = jsonObject.getString("access_token");
		System.err.println(token);
		jedisTemplate.opsForValue().set("esmobtoken", token);
	}
}
